package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.TuitionDao;
import com.example.domain.Tuition;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.TuitionService;
import com.example.tool.DateTool;


/**
 * @author 袁小弘
 * @describe 学费服务层接口
 * @date 2020-01-01
 */
@Service
public class TuitionServiceimpl implements TuitionService {

    @Autowired
    TuitionDao tuitionDao;

    /**
     * @describe 添加学费信息
     */
    @Override
    public Result addTuition(Tuition tuition) {
        if (tuition != null) {
            //获取系统当前时间作为记录创建时间
            tuition.setRecordTime(DateTool.getTimestamp());
        }
        //调用JPA保存实体方法
        Tuition tuition1 = tuitionDao.save(tuition);

        if (tuition1 != null) {
            return ResultFactory.buildSuccessResult(tuition1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新学费信息
     */
    @Override
    public Result updateTuition(Tuition tuition) {
        if (tuition != null) {
            //获取系统当前时间作为记录更新时间
            tuition.setUpdateTime(DateTool.getTimestamp());
        }
        //调用JPA保存并刷新实体方法
        Tuition tuition1 = tuitionDao.saveAndFlush(tuition);

        if (tuition1 != null) {
            return ResultFactory.buildSuccessResult(tuition1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号删除该学费信息
     */
    @Override
    public Result deleteTuitionById(String tuitionId) {

        int row = tuitionDao.deleteByTuitionId(Integer.parseInt(tuitionId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有学费信息
     */
    @Override
    public Result queryAllTuition() {

        List<Tuition> list = tuitionDao.findAll();

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询学费信息
     */
    @Override
    public Result queryTuitionById(String tuitionId) {

        List<Tuition> list = tuitionDao.findByTuitionId(Integer.parseInt(tuitionId));

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据学期ID匹配学期信息与学费信息
     */
    public Result queryTermAndTuitionAll() {
//		List<TermAndTuitionViewInfo> list = tuitionDao.findTermAndTuitionViewInfo();
        List<Map<String, Object>> list = tuitionDao.findTermAndTuitionViewInfo();
        /*
         * String timeStr = ""; list.forEach(a->{
         *
         * a.get("recordTime"); });
         */

        if (list != null) {
            System.out.println(list);
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加学费信息
     */
    @Override
    public Result addTuitionByExcel(List<Tuition> list) {
        List<Tuition> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = tuitionDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据学费编号批量删除学期信息
     */
    @Override
    public Result deleteTuitionByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = tuitionDao.deleteByTuitionIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 学费信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;
//		System.out.println(info[0]);
        if (info[0].equals("termName")) {

            list = tuitionDao.findByTermNameLike(info[1]);

        } else if (info[0].equals("price")) {

            list = tuitionDao.findByPriceLike(Integer.parseInt(info[1]));

        } else if (info[0].equals("userName")) {

            list = tuitionDao.findByUserNameLike(info[1]);

        } else if (info[0].equals("nickName")) {

            list = tuitionDao.findByNickNameLike(info[1]);

        } else if (info[0].equals("status")) {

            list = tuitionDao.findByStatusLike(info[1]);

        } else if (info[0].equals("className")) {

            list = tuitionDao.findByClassNameLike(info[1]);

        }

        System.out.println(info.toString() + "like==========" + list);

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result queryStudentIdNotInTuition() {
        // TODO 查询没有交费的班级学生
        List<Map<String, Object>> list = tuitionDao.queryStudentIdNotInTuition();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result selectTuitionByUserId(Integer userId) {
        // TODO 根据学生ID查询学费信息
        List<Map<String, Object>> list = tuitionDao.selectTuitionByUserId(userId);
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
